/* Copyright (c) 2007-2008, Jeffrey R Griffin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of Jeffrey R Griffin nor the
*       names of his contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Jeffrey R Griffin ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package net.visibleblue.android.restaurant;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

import net.visibleblue.android.app.dynamic.DynamicActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DetailActivity extends DynamicActivity{
	
	public static final ArrayList<OrderItem> OrderItems = new ArrayList<OrderItem>();
	private OrderItem _orderItem = null;

	@Override
	public int getThemeResource() {
		return android.R.style.Theme_White;
	}

	@Override
	public CharSequence getTitle() {
		return "Emily's Bistro - Menu";
	}

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.order_detail);
		
		_orderItem = MenuActivity.getOrderItem(this);
		LinearLayout layout = (LinearLayout)findViewById(R.id.item_layout);
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int width = dm.widthPixels;
		
		TextView price = new TextView(this);
		price.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		price.setPadding(5, 5, 5, 5);
		NumberFormat formatter = new DecimalFormat("#0.00");
		price.setText("$"+formatter.format(_orderItem.MenuItem.Price));
		
		if(_orderItem.MenuItem.Drawable!=-1)
		{
			LinearLayout vertLayout = new LinearLayout(this);
			vertLayout.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			vertLayout.setOrientation(LinearLayout.VERTICAL);
			ImageView image = new ImageView(this);
			image.setMaxWidth(100);
			image.setMaxHeight(100);
			image.setAdjustViewBounds(true);
			image.setImageDrawable(this.getResources().getDrawable(_orderItem.MenuItem.Drawable));
			image.measure(View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.AT_MOST), 
					View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.AT_MOST));
			width = width-image.getMeasuredWidth();
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(image.getMeasuredWidth(),
					image.getMeasuredHeight());
			params.gravity = Gravity.LEFT;
			vertLayout.addView(image, params);
			vertLayout.addView(price);
			layout.addView(vertLayout);
		}
		else
		{
			price.measure(0, 0);
			width = width - price.getMeasuredWidth();
			layout.addView(price);
		}
		
		TextView text = new TextView(this);
		text.setLayoutParams(new LinearLayout.LayoutParams(
				width, LayoutParams.WRAP_CONTENT));
		text.setPadding(5, 5, 5, 5);
		text.setText(_orderItem.MenuItem.Text);
		layout.addView(text, 0);
		
		Button addBtn = (Button)findViewById(R.id.add_button);
		addBtn.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				OrderItems.add(_orderItem);
				finish();
			}
			
		});
		
		Button cancelBtn = (Button)findViewById(R.id.cancel_button);
		cancelBtn.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				finish();
			}
			
		});
	}
}
